package org.alex.samples.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.model.UserVO;
import org.alex.samples.service.IScoreService;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.IUserService;
import org.alex.samples.service.ManageTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"/student"})
public class StudentController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IScoreService scoreService;
	
	@Autowired
	private ManageTestService testService;
	
	@Autowired
	ISubjectService subjectService;
	
	@RequestMapping(value="/subjects")
	public String toSubjectList(HttpSession session,Model model) throws Exception{
		UserVO userVO=(UserVO)session.getAttribute("user");
		List<SubjectVO> subjectList=userService.getSubjectList(userVO);
		model.addAttribute("subjectList",subjectList);
		return "student/studentSubjectList"; 
	}
	
	@ResponseBody
	@RequestMapping({ "/getScoreById" })
	public Map<String,Object> getScoreById(Integer scoreId) {
		Map<String,Object> map=new HashMap<String,Object>();	
		ExamScoreVO scoreVO=null;
		map.put("success", false);
		try {
			scoreVO = this.scoreService.getScoreById(scoreId);
			map.put("examTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(scoreVO.getExamTime()));
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return map;
	}
	
	@RequestMapping(value={"/toTest/{subId}"})
	public String getTestListBySubject(@PathVariable Integer subId,Model model,ModelMap modelMap,HttpSession session) throws Exception {
		UserVO userVO=(UserVO)session.getAttribute("user");
		Map<Integer, List<ExamTestVO>> map=testService.getTestListBySubject(subId,userVO.getId());
		modelMap.addAttribute("map", map);
		model.addAttribute("subject",subjectService.findSubjectById(subId));
		return "student/exam";
	}
	
	@RequestMapping(value={"/toTest/checkAnswer/{subId}"})
	public String checkAnswer(HttpServletRequest request, @PathVariable Integer subId, Model model, HttpSession session,
			RedirectAttributes redirect) throws Exception {
		UserVO userVO=(UserVO)session.getAttribute("user");
		Map<String,String[]> parameterMap=request.getParameterMap();
		Set<Entry<String,String[]>> entrySet=parameterMap.entrySet();
		List<Integer> answerIds=new ArrayList<Integer>();
		Map<String,String> testMap=new HashMap<String,String>();
		if(entrySet!=null){
			Iterator<Entry<String,String[]>> it=entrySet.iterator();
			final String TEST_PREFIX="test";
			while(it.hasNext()){
				Entry<String,String[]> entry=it.next();
				String key=entry.getKey();
				String[] splitArrayString=key.split("_");
				if(splitArrayString.length==2){
					if(TEST_PREFIX.equals(splitArrayString[0])){
						answerIds.add(Integer.valueOf(splitArrayString[1]));
						String aw="";
						for(String str:entry.getValue()){
							aw+=str;
						}
						testMap.put(splitArrayString[1], aw);
					}
				}
			}
			ExamScoreVO score=testService.checkAnswerAndSaveScore(subId,userVO.getId(),answerIds,testMap);
			redirect.addFlashAttribute("score", score);
		}
		return "redirect:/student/subjects";
	}

	

}
